package p23_类型转换与绑定.高层转换接口的五个实现;

import lombok.Data;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestParameterPropertyValues;

import java.util.Date;

/**
 * web环境下进行的数据绑定
 * 当需要时做类型转换，根据 directFieldAccess变量(boolean) 选择走 Property 还是 Field(同DataBinder，只不过数据源变成了请求对象)
 * 在前面的基础上，具备校验与获取校验结果的功能
 * 参数解析器：ServletModelAttributeMethodProcessor，就是使用了这个来进行类型转换与绑定
 */
public class _5_ServletRequestDataBinder {
    public static void main(String[] args) {
        MyBean bean = new MyBean();

        //ServletRequestDataBinder是DataBinder的子类
        ServletRequestDataBinder dataBinder = new ServletRequestDataBinder(bean);

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setParameter("a", "10");
        request.setParameter("b", "fdsfsd");
        request.setParameter("c", "1919/12/12");
        //数据来源变成请求对象
        //ServletRequestParameterPropertyValues是MutablePropertyValues的子类
        dataBinder.bind(new ServletRequestParameterPropertyValues(request));

        System.out.println(bean);
    }

    @Data
    static class MyBean {
        private int a;
        private String b;
        private Date c;
    }
}
